package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.EnLinkProductDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.enlink.EnLinkProduct;
import com.sls.game.service.EnLinkProductService;

@Service("enLinkProductService")
public class EnLinkProductServiceImpl implements EnLinkProductService {

	private static Logger log = Logger
			.getLogger(EnLinkProductServiceImpl.class);
	@Resource(name = "enLinkProductDAO")
	private EnLinkProductDAO enlinkDAO;
	private Gson gson;

	public EnLinkProductServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void save(String body, Head head) throws Exception {
		EnLinkProduct enlink = gson.fromJson(body, EnLinkProduct.class);
		this.validate(enlink);
		this.validateFeild(enlink);

		EnLinkProduct en = this.enlinkDAO.queryByProductId(enlink
				.getProductId());
		if (en == null) { // 插入操作
			enlink.setId(UUID.randomUUID().toString());
			enlink.setProductState("1");
			enlink.setIsAudit("0");
			enlink.setImportTime(ServerDateAndTime.getNowDateTimeOfFormat());
			try {
				this.enlinkDAO.insert(enlink);
			} catch (Exception e) {
				log.error(e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		} else { // 更新操作
			String id = en.getId();
			enlink.setId(id);
			enlink.setProductState("1");
			enlink.setIsAudit("0");
			enlink.setImportTime(ServerDateAndTime.getNowDateTimeOfFormat());
			try {
				this.enlinkDAO.update(enlink);
			} catch (Exception e) {
				log.error(e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

		}

	}

	@Override
	public Pagination<EnLinkProduct> queryEnLinkProduct(String body, Head head)
			throws ServerDisponseException {
		EnLinkProduct enlink = gson.fromJson(body, EnLinkProduct.class);
		this.validate(enlink);
		try {
			Pagination<EnLinkProduct> page = this.enlinkDAO
					.queryPaginationList(enlink);
			return page;
		} catch (Exception e) {
			log.error(e.toString(), e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public EnLinkProduct queryByProductId(String body, Head head)
			throws Exception {
		EnLinkProduct enlink = gson.fromJson(body, EnLinkProduct.class);
		this.validate(enlink);
		String productId = enlink.getProductId();
		if (StringUtils.isEmpty(productId)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PRODCODE_EMPTY.getCode(),
					ExceptionCodeDes.PRODCODE_EMPTY.getDescription());
		}

		try {
			EnLinkProduct elp = this.enlinkDAO.queryByProductId(productId);
			return elp;
		} catch (Exception e) {
			log.error(e.toString(), e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public void update(String body, Head head) throws Exception {
		EnLinkProduct enlink = gson.fromJson(body, EnLinkProduct.class);
		this.validate(enlink);
		String inPrice = enlink.getProductInPrice();
		if (StringUtils.isEmpty(inPrice)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getCode(),
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getDescription());
		}
		try {
			this.enlinkDAO.update(enlink);
		} catch (Exception e) {
			log.error(e.toString(), e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	/**
	 * 校验是否为null
	 * 
	 * @param obj
	 * @throws ServerDisponseException
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	private void validateFeild(EnLinkProduct enlink)
			throws ServerDisponseException {
		if (StringUtils.isEmpty(enlink.getProductId())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PRODCODE_EMPTY.getCode(),
					ExceptionCodeDes.PRODCODE_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(enlink.getProductName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.EN_LINK_PRODNAME_EMPTY.getCode(),
					ExceptionCodeDes.EN_LINK_PRODNAME_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(enlink.getProductInPrice())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getCode(),
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getDescription());
		} else if (StringUtils.isEmpty(enlink.getProductParvalue())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getCode(),
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getDescription());
		} else if (StringUtils.isEmpty(enlink.getProductType())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.RECHARGE_MODE_EMPTY.getCode(),
					ExceptionCodeDes.RECHARGE_MODE_EMPTY.getDescription());
		}
	}

}
